<!--
 * @Author: hq
 * @Date: 2022-05-28 19:29:52
 * @LastEditors: hq
 * @LastEditTime: 2022-07-01 15:00:00
 * @Description: 添加物料弹窗
 * @version: 1.0
-->
<template>
  <div class="change">
    <el-row>
      <el-col :span="12">
        <el-form
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="物料名" prop="name">
            <el-input v-model="name" placeholder="请输入物料名"></el-input>
          </el-form-item>
          <el-form-item label="厂家" prop="factory_name">
            <el-input
              v-model="factory_name"
              placeholder="请输入厂家"
            ></el-input>
          </el-form-item>
          <el-form-item label="厂家联系电话" prop="factory_tel">
            <el-input
              v-model="factory_tel"
              placeholder="请输入厂家联系电话"
            ></el-input>
          </el-form-item>
          <el-form-item label="类别" :required="true">
            <el-select v-model="ClassifyId" placeholder="请选择">
              <el-option
                v-for="item in ClassifyArr"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="预览图" :required="true">
            <el-upload
              class="avatar-uploader"
              action=""
              :show-file-list="false"
              :http-request="upLoadServe"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确定</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { shopCategory, editClassification } from "@/api/mall_management";
import { upLoad } from "@/api/index";
export default {
  props: ["rowData"],
  data() {
    return {
      rowId: "",
      name: "",
      factory_name: "",
      factory_tel: "",
      ClassifyArr: [],
      ClassifyId: "",
      imageUrl: "",
      imageUrlId: "",
      rules: {
        name: [{ required: true, message: "请输入物料名", trigger: "blur" }],
        factory_name: [
          { required: true, message: "请输入厂家", trigger: "blur" },
        ],
        factory_tel: [
          { required: true, message: "请输入厂家联系电话", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async editClassification() {
      const { data, err } = await editClassification({
        id: this.rowId,
        pid: this.ruleForm.upClassifyId,
        name: this.ruleForm.name,
        icon: this.ruleForm.imageUrlId,
        sort: this.ruleForm.sort,
        status: this.ruleForm.status,
        sid: 7,
      });
      if (err === 0) {
        if (this.rowId === "") {
          this.$message({
            showClose: true,
            message: "新增成功",
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: "编辑成功",
            type: "success",
          });
        }
        this.$emit("handleClose");
      }
    },
    //上传图片
    async upLoad(formData) {
      const { data, err } = await upLoad(formData);
      if (err === 0) {
        this.imageUrl = data.file_src;
        this.imageUrlId = data.file_id;
      }
    },
    //商品类型
    async shopCategory() {
      const { data, err } = await shopCategory({});
      if (err === 0) {
        this.upClassifyArr = this.upClassifyArr.concat(data);
        // this.upClassifyArr = data;
        console.log(this.upClassifyArr);
        this.ruleForm.upClassifyId = this.rowData.pid;
      }
    },
    upLoadServe(file) {
      let formData = new FormData();
      formData.append("file", file.file);
      this.upLoad(formData);
    },
    submitForm() {
      this.$emit("handleClose");
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //接口
        }
      });
    },
  },
  created() {
    this.shopCategory();
    if (this.rowData.id) {
      //编辑
      this.ruleForm = this.rowData;
      this.ruleForm.imageUrlId = this.rowData.logo.at_id;
      console.log(this.rowData.logo.src);
      this.ruleForm.imageUrl = this.rowData.logo.src;
      this.rowId = this.rowData.id;
      //   this.upClassifyArr = this.upClassifyArr.concat(this.rowData.children);
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
